package com.twu.biblioteca;

/**
 * Created with IntelliJ IDEA.
 * User: ruchita
 * Date: 22/9/13
 * Time: 10:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class CreateLibraryUser {
    public String nextLibraryNumber="";
    public String generateLibraryNumber(String currentLibraryNumber) {
        //for first user of library and after limit of library users exceed
        // here it is assumed that after limit has been reached the library number are restarted  from 111-1111
        if(currentLibraryNumber.equals("") || currentLibraryNumber.equals("999-9999") )
           return  nextLibraryNumber="111-1111";

        //After first users we have to provide last users library number to create next users library number
        else
        {
            //condition for second parts limit crossed
            if(getSecondPart(currentLibraryNumber).equals("9999"))
            {
                return nextLibraryNumber=String.valueOf(Integer.parseInt(getFirstPart(currentLibraryNumber))+1)+"-"+"0000";
            }
            else
                return nextLibraryNumber= getNextLibraryNumber(currentLibraryNumber);
        }

    }
    private String getNextLibraryNumber(String currentLibraryNumber) {

        String s = getFirstPart(currentLibraryNumber) + "-" + String.format("%04d",Integer.parseInt(getSecondPart(currentLibraryNumber)) + 1);
        return s;
    }
    private String getSecondPart(String currentLibraryNumber){
        return  currentLibraryNumber.split("-")[1];
    }
    private String getFirstPart(String currentLibraryNumber){
        return  currentLibraryNumber.split("-")[0];
    }
}
